package com.ruoyi.door.controller.admin.orderinfo;

import cn.hutool.core.date.DateUtil;
import com.easy.query.api.proxy.client.EasyEntityQuery;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.door.domain.DoorHouse;
import com.ruoyi.door.domain.DoorOrder;
import com.ruoyi.door.domain.proxy.DoorOrderProxy;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Api(value ="订单统计",tags = "订单统计")
@RestController
@RequestMapping("/door/order-static")
public class DoorOrderStaticController {
    @Autowired
    private EasyEntityQuery easyEntityQuery;

    /**
     * 统计昨日订单数据
     * 
     * @return 昨日结算金额、订单数、退款金额
     */
    @GetMapping("/info")
    @ApiOperation(value = "统计昨日订单数据", notes = "统计昨日结算金额、订单数、退款金额")
    public AjaxResult getYesterdayStats() {
        long totalCount = easyEntityQuery.queryable(DoorOrder.class)
                .count();
        // 计算昨天的开始和结束时间
        LocalDate yesterday = LocalDate.now().minusDays(1);
        LocalDate today = LocalDate.now();


        // 统计昨日已付款订单总金额
        BigDecimal yesterdaySettlementAmount = easyEntityQuery.queryable(DoorOrder.class)
                .where(x ->{
                    x.payTime().format("yyyy-MM-dd").eq(yesterday.toString());
                    x.orderStatus().eq(1L);
                })
                .sumBigDecimalOrDefault(DoorOrderProxy::totalPrice, BigDecimal.ZERO);
        
        // 统计昨日订单数
        long yesterdayOrderCount = easyEntityQuery.queryable(DoorOrder.class)
                .where(x ->{
                    x.payTime().format("yyyy-MM-dd").eq(yesterday.toString());
                })
                .count();
        long todayOrderCount = easyEntityQuery.queryable(DoorOrder.class)
                .where(x ->{
                    x.payTime().format("yyyy-MM-dd").eq(today.toString());
                })
                .count();
        long endCount = easyEntityQuery.queryable(DoorHouse.class)
                .where(x ->{
                    x.endDate().format("yyyy-MM-dd").lt(today.toString());
                })
                .count();
        long houseCount = easyEntityQuery.queryable(DoorHouse.class)
                .count();
        // 统计昨日退款金额
        BigDecimal yesterdayRefundAmount = easyEntityQuery.queryable(DoorOrder.class)
                .where(x ->{
                    x.refundCreateTime().format("yyyy-MM-dd").eq(yesterday.toString());
                    x.orderStatus().eq(3L);
                })
                .sumBigDecimalOrDefault(DoorOrderProxy::totalPrice, BigDecimal.ZERO);



        
        // 构建返回结果
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("yesterdayPayAmount", yesterdaySettlementAmount);
        resultMap.put("yesterdayOrderCount", yesterdayOrderCount);
        resultMap.put("yesterdayRefundAmount", yesterdayRefundAmount);
        resultMap.put("totalCount", totalCount);
        resultMap.put("todayOrderCount", todayOrderCount);
        resultMap.put("endDateCount", endCount);
        resultMap.put("houseCount", houseCount);

        return AjaxResult.success(resultMap);
    }

    public static void main(String[] args) {
        String startDate = DateUtil.yesterday().toDateStr();
        System.out.println(startDate.toString());
        LocalDate yesterday = LocalDate.now().minusDays(1);
        System.out.println(yesterday.toString());
    }
}
